'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));


module.exports.studentList = function(req,res){

    //判断是否登录
    CommonHelper.isLogin(req,res);

    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 10;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('student').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('student').alias('stu').join({
            table: 'class',
            join: 'left',
            as: 'class',
            on: ['class_id', 'class_id']
        }).join({
            table: 'school',
            join: 'left',
            as: 'school',
            on: ['school_id', 'school_id']
        }).join({
            table: 'user',
            join: 'left',
            as: 'user',
            on: ['user_id', 'user_id']
        }).where(where).order('stu.stu_id DESC').limit(start,limit).select().then(function(student){

            let data = {
                array:student,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"studentList.html",data);
        });
    });
};

module.exports.studentAdd = function(req,res){
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('student').select().then(function(student){
        db.table('class').select().then(function(class_list){
            db.table('school').select().then(function(school){
                db.table('user').select().then(function(user){
                    let Data = {
                        "student":student,
                        "class":class_list,
                        "school":school,
                        "user":user
                    };

                    CommonHelper.renderFile(req,res,"studentAdd.html",Data);
                });
            });
        });        
    });
};

module.exports.studentAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据

    let data = {
        "stu_name":req.body.stu_name,
        "stu_number":req.body.stu_number,
        "class_id":req.body.class_id,
        "school_id":req.body.school_id,
        "user_id":req.body.user_id
    };

    db.table('student').add(data).then(function(insertId){
        if(insertId)
        {
            //重定向
            res.redirect(`/student/studentList`);
            return false;
        }else{
            //重定向
            res.redirect(`/student/studentAdd`);
            return false;
        }
    });
};

module.exports.studentEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let stu_id = req.query.stu_id;

    if(!stu_id)
    {
        //重定向
        res.redirect(`/student/studentList`);
        return false;
    }

    db.table('student').where(`stu_id = ${stu_id}`).find().then(function(student){
        db.table('class').select().then(function(class_list){
            db.table('school').select().then(function(school){
                let Data = {
                    "student":student,
                    "class":class_list,
                    "school":school
                };

                CommonHelper.renderFile(req,res,"studentEdit.html",Data);
            });
        });
    });
};

module.exports.studentEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let stu_id = req.query.stu_id;

    if(!stu_id)
    {
        //重定向
        res.redirect(`/student/studentList`);
        return false;
    }

    db.table('student').where(`stu_id = ${stu_id}`).find().then(function(student){

        let data = {
            "stu_name":req.body.stu_name,
            "stu_number":req.body.stu_number,
            "class_id":req.body.class_id,
            "school_id":req.body.school_id
        };

        db.table('student').where(`stu_id = ${stu_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/student/studentList`);
                return false;
            }else{
                //重定向
                res.redirect(`/student/studentEdit?stu_id=${stu_id}`);
                return false;
            }
        });
    });
};

module.exports.studentDelete = function(req,res){

    let stu_id = req.query.stu_id;
    let user_id = req.query.user_id;

    if(!stu_id || !user_id)
    {
        //重定向
        res.redirect(`/student/studentList`);
        return false;
    }
    
    db.table('student').where(`stu_id = ${stu_id}`).delete().then(function(affectId){
        if(affectId){
            db.table('user').where(`user_id = ${user_id}`).delete().then(function(affectId2){
                if(affectId2){
                    console.log("学生表和账号表删除成功");
                }else{
                    console.log("学生表删除成功，但账号表删除失败！");
                }
                
            });
        }else{
            console.log("删除学生表和账号表失败！");
        }

        
        //重定向
        res.redirect(`/student/studentList`);
        return false;
        
    });
};